import { PrismaClient } from '@prisma/client'
import bcrypt from 'bcryptjs'

const prisma = new PrismaClient()

async function main() {
    // 创建默认角色
    const roles = ['admin', 'user']

    for (const roleName of roles) {
        await prisma.role.upsert({
            where: { name: roleName },
            update: {},
            create: { name: roleName }
        })
    }

    // 创建默认管理员用户
    const adminRole = await prisma.role.findUnique({
        where: { name: 'admin' }
    })

    if (adminRole) {
        const hashedPassword = await bcrypt.hash(process.env.ADMIN_PASSWORD || 'admin123', 10)
        await prisma.user.upsert({
            where: { email: process.env.ADMIN_EMAIL || 'admin@example.com' },
            update: {},
            create: {
                email: process.env.ADMIN_EMAIL || 'admin@example.com',
                username: 'admin',
                password: hashedPassword,
                roleId: adminRole.id
            }
        })
        console.log('Default admin user created')
    }
}

main()
    .catch((e) => {
        console.error('Error seeding database:', e)
        process.exit(1)
    })
    .finally(async () => {
        await prisma.$disconnect()
    }) 